﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace CProd
{
    public partial class frmLinhas : Form
    {
        #region Declarações

        SqlConnection Conn = new SqlConnection();
        SqlDataAdapter daLines = new SqlDataAdapter();
        SqlCommandBuilder Builder = new SqlCommandBuilder();
        DataTable dtLines = new DataTable("dtLines");
        DataView dvLines = new DataView();
        BindingSource srcLines = new BindingSource();
        Boolean newRecord = false;

        #endregion

        public frmLinhas()
        {
            InitializeComponent();
        }

        private void Linhas_Load(object sender, EventArgs e)
        {
            Builder.DataAdapter = daLines;
            daLines.SelectCommand = new SqlCommand("Select * from Linhas", frmEntrada.conn);

            daLines.Fill(dtLines);

            srcLines.DataSource = dtLines.DefaultView;
            dvLines.Table = dtLines;

            txtCodigo.DataBindings.Add("Text", srcLines, "Prod_Lin");
            txtNome.DataBindings.Add("Text", srcLines, "Prod_Des");

            GridLinhas.DataSource = srcLines;
            GridLinhas.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            GridLinhas.Columns[0].HeaderText = "Número";
            GridLinhas.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
            GridLinhas.Columns[1].HeaderText = "Designação";
        }

        private void btnSair_Click(object sender, EventArgs e)
        { Close(); }

        private void btInsert_Click(object sender, EventArgs e)
        {
            txtCodigo.ReadOnly = false;
            txtCodigo.Text = "";
            txtNome.Text = "";
            txtCodigo.Focus();
            newRecord = true;
        }

        private void btSave_Click(object sender, EventArgs e)
        {
            if (txtCodigo.TextLength < 2) txtCodigo.Text = '0' + txtCodigo.Text;

            txtCodigo.ReadOnly = true;

            try
            {
                #region novo registo

                if (newRecord)
                {
                    dvLines.RowFilter = string.Format("Prod_Lin='{0}'", txtCodigo.Text);
                    if (dvLines.Count > 0)
                    {
                        MessageBox.Show("Linha de Produção já existente");
                        newRecord = false;
                    }
                    else
                    {
                        DataRow dRow = dtLines.NewRow();
                        dRow[0] = txtCodigo.Text;
                        dRow[1] = txtNome.Text;
                        dtLines.Rows.Add(dRow);
                    }
                }
                #endregion
                else BindingContext[srcLines].EndCurrentEdit();
                daLines.Update(dtLines);

                //Actualiza Dados Mostrados
                dtLines.Clear();
                daLines.Fill(dtLines);
            }
            catch (Exception ex)
            { MessageBox.Show(ex.Message); }
        }

        private void btDelete_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Tem a certeza?", "Apagar Registo", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                try
                {
                    srcLines.RemoveCurrent();
                    daLines.Update(dtLines);
                    MessageBox.Show("Registo Apagado com Sucesso");
                }
                catch (Exception ex)
                { MessageBox.Show(ex.Message); }
            }
        }

        #region navigate records

        private void btFirst_Click(object sender, EventArgs e)
        {
            srcLines.MoveFirst();
        }

        private void btPrev_Click(object sender, EventArgs e)
        {
            srcLines.MovePrevious();
        }

        private void btNext_Click(object sender, EventArgs e)
        {
            srcLines.MoveNext();
        }

        private void btLast_Click(object sender, EventArgs e)
        {
            srcLines.MoveLast();
        }

        #endregion

        private void txtCodigo_KeyPressed(object sender, KeyPressEventArgs e)
        { if (char.IsLetter(e.KeyChar)) e.Handled = true; }
    }
}
